package ee.maoinvest.frontend.controller.requestprocessors;

import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import ee.maoinvest.backend.model.data.ElectricityMeter;
import ee.maoinvest.backend.model.data.Substation;
import ee.maoinvest.frontend.controller.formstringdataconverters.substationforms.DeleteSubstationFormConverter;
import ee.maoinvest.frontend.controller.formstringdataconverters.substationforms.EditSubstationFormConverter;
import ee.maoinvest.frontend.controller.formstringdataconverters.substationforms.InsertSubstationFormConverter;

public class SubstationFormsRequestProcessor 
{
	private final String EMPTY_STRING = "";
	private final int GET_EDITABLE_SUBSTATION_ID_PARSE_EXCEPTION_INDICATOR = -1;
	
	public InsertSubstationFormConverter fillInsertSubstationFormConverterWithRequestData(
			HttpServletRequest request)
	{
		InsertSubstationFormConverter insertSubstationFormConverter = 
				new InsertSubstationFormConverter();
		//getting data from form, that has been put on request
		String substationName = request.getParameter("insertSubstationName");
		String dateInserted = request.getParameter("insertSubstationDateInserted");		
		
		HttpSession session = request.getSession();
		Date latestDateAfterWhatDataManipulationIsPossible = (Date) session.getAttribute("latestDateAfterWhatDataManipulationIsPossible");
		
		insertSubstationFormConverter.setSubstationName(substationName);
		insertSubstationFormConverter.setDateInserted(dateInserted);
		insertSubstationFormConverter.setLatestDateAfterWhatDataManipulationIsPossible(latestDateAfterWhatDataManipulationIsPossible);
		
		return insertSubstationFormConverter;
	}
	
	
	public EditSubstationFormConverter fillEditSubstationFormConverterWithRequestData(
			HttpServletRequest request)
	{
		EditSubstationFormConverter editSubstationFormConverter = 
				new EditSubstationFormConverter();
		
		//getting data from form, that has been put on request
		//Siinne postitatud nimi ID saamiseks ei ole substationIDToEditForm, vaid substationID, sest 
		//tegemist on kahe eraldi vormiga. substationIDToEditForm on saadetud getAllSubstations alt. 
		//siinne, aga editSubstation formi alt
		String substationID = request.getParameter("substationID");
		String substationName = request.getParameter("editSubstationName");	
		HttpSession session = request.getSession();
		Substation substationToEdit = (Substation) session.getAttribute("substationToEdit");
		
		editSubstationFormConverter.setSubstationID(substationID);
		editSubstationFormConverter.setSubstationName(substationName);
		editSubstationFormConverter.setSubstationToEdit(substationToEdit);
		
		return editSubstationFormConverter;
	}
	
	public DeleteSubstationFormConverter fillDeleteSubstationFormConverterWithRequestData(HttpServletRequest request)
	{
		DeleteSubstationFormConverter deleteSubstationFormConverter = new DeleteSubstationFormConverter();
		
		String substationIDThatIsChosenToDelete = request.getParameter("substationIDThatIsChosenToDelete");
		
		HttpSession session = request.getSession();
		Substation substationBeingDeleted = (Substation) session.getAttribute("substationBeingDeleted");
		@SuppressWarnings("unchecked")
		List<ElectricityMeter> listOfAllSubstationMainElectricityMetersConnectedWithSubstation = 
				(List<ElectricityMeter>) session.getAttribute("listOfAllSubstationMainElectricityMetersConnectedWithSubstation");
	
		deleteSubstationFormConverter.setSubstationIDThatIsChosenToDelete(substationIDThatIsChosenToDelete);
		deleteSubstationFormConverter.setSubstationBeingDeleted(substationBeingDeleted);
		deleteSubstationFormConverter.setListOfAllSubstationMainElectricityMetersConnectedWithSubstation(
				listOfAllSubstationMainElectricityMetersConnectedWithSubstation);
		
		return deleteSubstationFormConverter;
	}
	
	public int getEditableSubstationID(HttpServletRequest request)
	{
		int stringParsedToInt = GET_EDITABLE_SUBSTATION_ID_PARSE_EXCEPTION_INDICATOR;
		
		String substationID = request.getParameter("substationIDToEditForm");
		if (substationID != null && !substationID.equals(EMPTY_STRING))
		{
			try 
			{
				stringParsedToInt = Integer.parseInt(substationID);
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				return GET_EDITABLE_SUBSTATION_ID_PARSE_EXCEPTION_INDICATOR;
			}
		}
		
		return stringParsedToInt;
	}
	
	public int getSubstationIDToDelete(HttpServletRequest request)
	{
		int stringParsedToInt = GET_EDITABLE_SUBSTATION_ID_PARSE_EXCEPTION_INDICATOR;
		
		String substationID = request.getParameter("substationIDToDelete");
		if (substationID != null && !substationID.equals(EMPTY_STRING))
		{
			try 
			{
				stringParsedToInt = Integer.parseInt(substationID);
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				return GET_EDITABLE_SUBSTATION_ID_PARSE_EXCEPTION_INDICATOR;
			}
		}
		
		return stringParsedToInt;
	}
	
}
